<?php
/*
 * @Author: 李明
 * @Date: 2021-09-06 11:04:21
 * @LastEditTime: 2021-12-10 15:51:23
 * @LastEditors: 李明
 * @Description: 
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/script/UpdateScript.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\script;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Controllers\order\OrderController;
use App\Models\Image;
use App\Models\InterestRecord;
use App\Models\Order;
use App\Models\User;
use App\Models\Worker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UpdateScript extends Controller
{
    /**
     * @description: 更新利息记录的期数
     * @param {Request} $request
     * @return {*}
     */
    public function updateInterestNum(Request $request)
    {
        if (!env('APP_DEBUG', 'false')) {
            return LMReturn::Error('禁止调用', 'US001');
        }
        $interest_list = InterestRecord::all();
        foreach ($interest_list as $interest) {
            $interest_list_sum = InterestRecord::where('order_id', $interest['order_id'])
                ->orderBy('date', 'asc')
                ->get();
            $num_all = count($interest_list_sum);
            foreach ($interest_list_sum as $i => $interest_sum) {
                $interest_sum->num_all = $num_all;
                $interest_sum->num = $i + 1;
                $interest_sum->save();
            }
        }
        return '更新了' . count($interest_list) . '条数据';
    }
    /**
     * @description: 给已生效订单插入利息记录
     * @param {Request} $request
     * @return {*}
     */
    public function updateOrderInterest(Request $request)
    {
        if (!env('APP_DEBUG', 'false')) {
            return LMReturn::Error('禁止调用', 'US001');
        }
        $order_list = Order::all();
        foreach ($order_list as $order) {
            $interest_list = InterestRecord::where('order_id', $order->id)->get();
            if (count($interest_list) > 0) {
                Log::debug('订单id为' . $order->id . '的订单无需增加利息记录,原因是已经生成了记录');
            } else {
                if ($order->order_status == '4') { // 订单状态 -1草稿， 1是待付款  2是审核中   3是待生效(弃用)  4是已生效 5是拒绝  6是已结束   
                    // $this->createOrderInterest($order);
                } else {
                    Log::debug('订单id为' . $order->id . '的订单无需增加利息记录,原因是订单不在生效状态');
                }
            }
        }
    }
    /**
     * @description: 插入利息记录
     * @param {*} $order
     * @return {*}
     */
    public static function createOrderInterest($order)
    {
        if (!env('APP_DEBUG', 'false')) {
            return LMReturn::Error('禁止调用', 'US001');
        }
        $product = $order->product;
        // 如果是需要付款的订单，则进行插入付款记录
        if ($product->pay_type != '5') { // 付款方式  2自然月  4到期 5不付款
            // 如果是按月付款，根据付款间隔时长生成对应的付款记录
            $payLong = $product->pay_long;
            $longTime = $product->long_time; // 产品周期
            $orderMoney = $order->order_money; // 本金
            $orderID = $order->id;
            $endTime = date("Y-m-d", strtotime($order->contract_end_date)); // 到期时间
            $validTime = date("Y-m-d", strtotime($order->valid_time)); // 生效时间
            $rate = $product->rate;
            // 如果需要付款，判断是到期付款还是按月付款
            $resArr = [];
            // 插入订单打款记录
            if ($product->pay_type == '2') { //自然月   
                $resArr = OrderController::createOrderMonthInterest($longTime, $payLong, $orderMoney, $rate, $validTime, $orderID, $endTime, $order->branch_id);
            } elseif ($product->pay_type == '4') { //到期付款
                // $resArr = OrderController::createOrderEndPay($orderID, $endTime, $orderMoney, $rate, $longTime, $branch_id);
            }
        }
        Log::debug('订单id为' . $orderID . '的订单增加记录成功增加利息记录');
        Log::debug('记录信息是');
        Log::debug($resArr);
    }
    public function updateNoHttp(Request $request)
    {
        $where = [];
        $where[] = ['img_path', 'not like', 'http%'];
        $image_list = Image::where($where)->get();
        foreach ($image_list as $image) {
            if (env('APP_DEBUG', 'true')) {
                $url_head = 'https://bd.php.huaxinestates.com/storage/';
            } else {
                $url_head = 'https://hx.api.1024xxkj.com/storage/';
            }
            $image->url = $url_head . $image->img_path;
            $image->save();
        }
    }
    public function updateAccountStatus(Request $request)
    {
        $where = [];
        $worker_list = Worker::whereNull('account_status')->get();
        foreach ($worker_list as $worker) {
            $worker->account_status = 1;
            $worker->save();
        }
        return   $worker_list;
    }
    public function updateAccountStatusTwo(Request $request)
    {
        $where = [];
        $worker_list = Worker::where('user_status', '0')->get();
        foreach ($worker_list as $worker) {
            $worker->account_status = 1;
            $worker->save();
        }
        return   $worker_list;
    }
}
